package com.squirrel.common.controller;

import com.squirrel.common.domain.WebSocketServer;
import com.squirrel.common.param.danmu.SendParam;
import com.squirrel.common.service.CommentarySubtitlesService;
import com.squirrel.common.response.ResponseResult;
import com.squirrel.common.response.ResponseResultUtils;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.util.BiliBiliDanmuUtils;
import com.squirrel.common.util.HttpRequest;
import com.squirrel.common.vo.CommentaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * CommentarySubtitlesController 弹幕控制器
 * </p>
 *
 * @author 关远键
 * @since 2021/11/11 16:06
 */
@RestController
@RequestMapping("/api/danmu")
@Api(tags = "弹幕控制器")
public class CommentarySubtitlesController {
    @Autowired
    private CommentarySubtitlesService subtitlesService;
    @Autowired
    private WebSocketServer webSocketServer;

    @GetMapping("/getDanmu/v3")
    @ApiOperation("获取弹幕")
    public ResponseResult<List> getDanmu(@RequestParam Integer id) {
        List<CommentaryVO> commentary = subtitlesService.getAllCommentary(id);
        return ResponseResultUtils.danmuSuccess(BiliBiliDanmuUtils.extractFromXML(id, commentary));
    }

    @PostMapping("/getDanmu/v3")
    @ApiOperation("发送弹幕")
//    @AuthToken    // 必须是已登录的用户
    public ResponseResult<Boolean> sendDanmu(@RequestBody() SendParam sendParam) throws IOException {
        webSocketServer.sendAllDanmu(sendParam.getId(), sendParam);
        return ResponseResultUtils.danmuSuccess(subtitlesService.sendCommentary(sendParam));
    }

}
